//
//  TextViewController.m
//  Gunz
//
//  Created by Joggy on 15/12/12.
//  Copyright © 2015年 NUPT. All rights reserved.
//

#import "TextViewController.h"
#import "TaskDetailViewController.h"
#import "ReleaseStep1ViewController.h"
#import "LightSensorViewController.h"
#import "QSViewController.h"
#import "VCViewController.h"
#import "VDViewController.h"
#import "IMViewController.h"

@implementation TextViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    menu0 = [[NSArray alloc] initWithObjects:@"智能排序", @"任务排序", @"任务类型", @"所属行业", nil];
    menu1 = [[NSArray alloc] initWithObjects:@"酬劳最高", @"离我最近", @"最新上线", nil];
    menu2 = [[NSArray alloc] initWithObjects:@"任务1", @"任务2", @"任务3", nil];
    menu3 = [[NSArray alloc] initWithObjects:@"类型1", @"类型2", @"类型3", @"类型4", nil];
    menu4 = [[NSArray alloc] initWithObjects:@"行业1", @"行业2", @"行业3", @"行业4", @"行业5", @"行业6", @"行业7", @"行业8", @"行业9", @"行业10", @"行业11", @"行业12", nil];
    menu = [[NSMutableArray alloc] init];
    for(int i = 0; i < menu0.count; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeSystem];
        but.frame = CGRectMake(i*(screenWidth/4), 0, screenWidth/4, TVCTextStyleHeight);
        NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:menu0[i]];
        NSRange titleRange = {0, title.length};
        [title addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.3 green:0.32 blue:0.34 alpha:1] range:titleRange];
        [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font16] range:titleRange];
        but.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        but.tag = i;
        [but setAttributedTitle:title forState:UIControlStateNormal];
        [but addTarget:self action:@selector(dropMenu:) forControlEvents:UIControlEventTouchUpInside];
        [_textStyle addSubview:but];
        [menu addObject:but];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i*(screenWidth/4), TVCTextStyleHeight/4, 1, TVCTextStyleHeight/2)];
        line.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        [_textStyle addSubview:line];
    }
    dm = [[DMView alloc] init];
    //刷新控件
    _tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    _tb.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    [_tb.mj_header beginRefreshing];
    
    //工具包
    tookkit = @[@"光线传感器", @"问卷调查表", @"音频", @"视频", @"图片"];
}

-(void)prepareForNav{
    [super prepareForNav];
    self.title = @"任务";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"task_release"];
    leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(releaseItem) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
    //导航右按键
    QYOptionButton *rightButton = [[QYOptionButton alloc] init];
    rightButton.delegate = self;
    [rightButton setImage:[UIImage imageNamed:@"all_edit"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    _textStyle = [[UIView alloc] initWithFrame:CGRectMake(-1, 0, screenWidth + 2, TVCTextStyleHeight)];
    _textStyle.backgroundColor = [UIColor whiteColor];
    _textStyle.layer.borderWidth = 1;
    _textStyle.layer.borderColor = [UIColor colorWithRed:0.78 green:0.78 blue:0.78 alpha:1].CGColor;
    [self.view addSubview:_textStyle];
    //tableview
    _tb = [[UITableView alloc] initWithFrame:CGRectMake(0, TVCTextStyleHeight, screenWidth, screenHeight - 64 - TVCTextStyleHeight)];
    _tb.backgroundColor = [UIColor clearColor];
    [_tb registerClass:[MVTableViewCell class] forCellReuseIdentifier:@"tvcCell"];
    _tb.delegate = self;
    _tb.dataSource = self;
    _tb.showsVerticalScrollIndicator = false;
    _tb.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view insertSubview:_tb belowSubview:_textStyle];
}

- (void)headerRefresh {
    NSDictionary *para = @{@"lastid": @"0"};
    __weak typeof (self) weakSelf = self;
    [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8001/task/getListById" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic1 = responseObject;
        data = dic1[@"msg"];
        if(data.count <= 0) {
            data = [weakSelf defaultData];
        }
        [_tb reloadData];
        [_tb.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        data = [weakSelf defaultData];
        [_tb reloadData];
        [_tb.mj_header endRefreshing];
    }];
}

- (void)footerRefresh {
    [_tb.mj_footer endRefreshing];
}

- (void)dropMenu:(UIButton*)button {
    switch(button.tag) {
        case 0:
            if ([DMView DMStatus] == 8) {
                [dm dismiss];
                [DMView setDMStatus:0];
            }
            else {
                [dm dismiss];
                dm = [[DMView alloc] initWithFrame:CGRectMake(0, 0, screenWidth/4, screenHeight/2) andMenu:menu1];
                [self showDM];
                [DMView setDMStatus:8];
            }
            break;
        case 1:
            if ([DMView DMStatus] == 4) {
                [dm dismiss];
                [DMView setDMStatus:0];
            }
            else {
                [dm dismiss];
                dm = [[DMView alloc] initWithFrame:CGRectMake(screenWidth/4, 0, screenWidth/4, screenHeight/2) andMenu:menu2];
                [self showDM];
                [DMView setDMStatus:4];
            }
            break;
        case 2:
            if ([DMView DMStatus] == 2) {
                [dm dismiss];
                [DMView setDMStatus:0];
            }
            else {
                [dm dismiss];
                dm = [[DMView alloc] initWithFrame:CGRectMake(2*screenWidth/4, 0, screenWidth/4, screenHeight/2) andMenu:menu3];
                [self showDM];
                [DMView setDMStatus:2];
            }
            break;
        case 3:
            if ([DMView DMStatus] == 1) {
                [dm dismiss];
                [DMView setDMStatus:0];
            }
            else {
                [dm dismiss];
                dm = [[DMView alloc] initWithFrame:CGRectMake(3*screenWidth/4, 0, screenWidth/4, screenHeight/2) andMenu:menu4];
                [self showDM];
                [DMView setDMStatus:1];
            }
            break;
        default:
            break;
    }
}

-(void)showDM {
    dm.alpha = 0;
    [self.view insertSubview:dm aboveSubview:_tb];
    [UIView animateWithDuration:0.3 animations:^{
        dm.alpha = 1;
        CGRect temFrame = dm.frame;
        temFrame.origin.y = TVCTextStyleHeight;
        dm.frame = temFrame;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)releaseItem {
    ReleaseStep1ViewController *releaseItem = [[ReleaseStep1ViewController alloc] init];
    UINavigationController *releaseNav = [[UINavigationController alloc] initWithRootViewController:releaseItem];
    [self presentViewController:releaseNav animated:true completion:nil];
}

#pragma 代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if ([DMView DMStatus] != 0) {
        [dm dismiss];
        [DMView setDMStatus:0];
    }
    NSDictionary *tep = data[indexPath.section];
    if([tep[@"icontask"] isEqualToString:@"__no"]) {
        TaskDetailViewController *detail = [[TaskDetailViewController alloc] init];
        detail.isMineRelease = false;
        detail.hidesBottomBarWhenPushed = true;
        detail.data = tep[@"fields"];
        [self.navigationController pushViewController:detail animated:true];
    }
    else {
        NSDictionary *para = @{@"id": tep[@"id"], @"parenttask_id": tep[@"parenttask_id"]};
        [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8001/task/taskDetail" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic1 = responseObject;
            NSArray *arr1 = dic1[@"msg"];
            NSDictionary *dic2 = arr1[0];
            NSDictionary *dic3 = dic2[@"fields"];
            NSLog(@"%@", dic3);
            TaskDetailViewController *detail = [[TaskDetailViewController alloc] init];
            detail.isMineRelease = false;
            detail.hidesBottomBarWhenPushed = true;
            detail.data = dic3;
            [self.navigationController pushViewController:detail animated:true];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            //        NSLog(@"%@", error);
            
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return data.count;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, MVCInsert)];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return MVCInsert;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return MVCCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = (MVTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"tvcCell"];
    NSDictionary *dic = data[indexPath.section];
    NSString *imgURL = @"http://180.209.64.49:8001/";
    imgURL = [imgURL stringByAppendingString:dic[@"icontask"]];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"插图1"]];
    cell.cellTitle.text = dic[@"name"];
    NSString *detailStr = dic[@"describe"];
    cell.detail.text = detailStr.description;
    NSString *priceStr = dic[@"faretask"];
    priceStr = [priceStr.description stringByAppendingString:@"¥"];
    cell.priceLabel.text = priceStr;
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if ([DMView DMStatus] != 0) {
        [dm dismiss];
        [DMView setDMStatus:0];
    }
}

#pragma 导航右按键代理方法

- (NSInteger)QYOptionNumberOfOptionButtonItem:(QYOptionButton *)optionButton {
    return tookkit.count;
}

- (QYOptionItem*)QYOptionItemAtIndex:(QYOptionButton *)optionButton itemAtIndex:(NSInteger)index {
    QYOptionItem *item = [[QYOptionItem alloc] initWithFrame:CGRectMake(0, 0, 64, 88)];
    [item.button setImage:[UIImage imageNamed:@"插图1"] forState:UIControlStateNormal];
    item.title.text = tookkit[index];
    switch (index) {
        case 0:
            [item.button addTarget:self action:@selector(lightSensor) forControlEvents:UIControlEventTouchUpInside];
            break;
        case 1:
            [item.button addTarget:self action:@selector(questionnaire) forControlEvents:UIControlEventTouchUpInside];
            break;
        case 2:
            [item.button addTarget:self action:@selector(voice) forControlEvents:UIControlEventTouchUpInside];
            break;
        case 3:
            [item.button addTarget:self action:@selector(video) forControlEvents:UIControlEventTouchUpInside];
            break;
        case 4:
            [item.button addTarget:self action:@selector(tkImage) forControlEvents:UIControlEventTouchUpInside];
        default:
            break;
    }
    return item;
}

- (void)lightSensor {
    LightSensorViewController *lightSensor = [[LightSensorViewController alloc] init];
    UINavigationController *lightSensorNav = [[UINavigationController alloc] initWithRootViewController:lightSensor];
    [self presentViewController:lightSensorNav animated:true completion:nil];
}

- (void)questionnaire {
    QSViewController *qsView = [[QSViewController alloc] init];
    UINavigationController *qsNav = [[UINavigationController alloc] initWithRootViewController:qsView];
    [self presentViewController:qsNav animated:true completion:nil];
}

- (void)voice {
    VCViewController *vcView = [[VCViewController alloc] init];
    UINavigationController *vcNav = [[UINavigationController alloc] initWithRootViewController:vcView];
    [self presentViewController:vcNav animated:true completion:nil];
}

- (void)video {
    VDViewController *vdView = [[VDViewController alloc] init];
    UINavigationController *vdNav = [[UINavigationController alloc] initWithRootViewController:vdView];
    [self presentViewController:vdNav animated:true completion:nil];
}

- (void)tkImage {
    IMViewController *imView = [[IMViewController alloc] init];
    UINavigationController *imNav = [[UINavigationController alloc] initWithRootViewController:imView];
    [self presentViewController:imNav animated:true completion:nil];
}

- (NSArray*)defaultData {
    
    NSArray* array = @[
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"__no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           },
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"__no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           },
                       @{
                           @"icontask": @"__no",
                           @"name": @"南京本地话语音采集",
                           @"describe": @"采集南京本地话",
                           @"faretask": @"16",
                           @"fields": @{
                                   @"icontask": @"__no",
                                   @"name": @"南京本地话语音采集",
                                   @"faretask": @"16",
                                   @"ownuser": @"简果公司",
                                   @"starttime": @"2016",
                                   @"overtime": @"2016",
                                   @"describe": @"采集南京本地话",
                                   @"templete": @"无"
                                   }
                           }
                       ];
    
    return array;
}

//#pragma UIImagePickerController代理方法

//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//    if(picker.cameraCaptureMode == UIImagePickerControllerQualityTypeMedium) {
//        NSLog(@"%@", info);
//    }
//}

@end
